<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="600px">
      <el-form :model="formReducedUnit" label-position="right" :rules="rules" ref="formReducedUnit">
        <el-row>
          <el-col :span="24">
            <el-form-item label="换算系数" prop="measrate" :label-width="formLabelWidth">
              <el-input v-model="formReducedUnit.measrate" placeholder="请输入换算比例"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="单位" prop="pK_MEASDOC" :label-width="formLabelWidth">
              <el-select clearable v-model="formReducedUnit.pK_MEASDOC" style="width: 100%;">
                <el-option v-for="(item, index) in wuLiaoList" :key="index" :value="item.pkMeasdoc" :label="item.unitName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  nccBasicReducedUnitSave,
  findNccBasicUnitAll,
  nccBasicReducedUnitUpdate
} from '@/api/basic/PersonnelInformation.js'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formReducedUnit: {
        measrate: "",
        pK_MEASDOC: ""
      },
      wuLiaoList: [],
      title: '新增物料系数',
      formLabelWidth: '110px',
      rules: {
        measrate: [{
          required: true,
          message: '请输入换算比例',
          trigger: 'blur'
        }],
        pK_MEASDOC: [{
          required: true,
          message: '请输入计量单位',
          trigger: 'blur'
        }]
      },
    };
  },
  created(){
    this.getWuLiao();
  },
  methods: {
    //物料单位下拉
    getWuLiao() {
      findNccBasicUnitAll().then(res => {
        this.wuLiaoList = res.data;
      })
    },
    //获取当前行信息
    openDialog(type, val) {
      this.title = type === 'edit' ? '编辑物料系数' : '新增物料系数';
      // 根据类型区分新增和编辑逻辑
      if (type === 'edit' && val) {
        this.formReducedUnit = {...val};
      } else {
        this.formReducedUnit = {
          measrate: "",
          pK_MEASDOC: ""
        }
      }
    },
    // 提取重置表单的逻辑
    handleClose() {
      // 通过事件通知父组件更新visible值，触发update:visible事件
      this.$emit('update:visible', false);
      this.$nextTick(() => {
        this.$refs.formReducedUnit.resetFields();
      })
    },
    //提交表单
    submit() {
      this.$refs.formReducedUnit.validate((valid) => {
        if (valid) {
          const apiMethod = this.title === '新增物料系数' ? nccBasicReducedUnitSave : nccBasicReducedUnitUpdate;
          // 调用相应的 API 方法
          apiMethod(this.formReducedUnit).then(res => {
            this.$emit('addSuccess'); // 通知父组件添加成功
            this.$message({
              message: res.msg, // 显示成功消息
              type: 'success',
            });
            // 提交成功后可以选择清空表单
            this.handleClose();
          }).catch(err => {
            this.$message({
              message: err?.msg || '操作失败', // 捕获错误信息
              type: 'error',
            });
          });
        } else {
          // 校验失败
          return false;
        }
      });
    }
  },
};
</script>

<style></style>
